/**
 * Copyright 2016 vip.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 * </p>
 **/

package spring.cloud.tasks.api.database.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spring.cloud.tasks.api.database.entity.TaskConfigurationDto;
import spring.cloud.tasks.api.database.repository.HistoryTaskConfigDtoRepository;
import spring.cloud.tasks.api.database.service.HistoryTaskConfigurationDtoService;

import java.util.List;

@Service
public class HistoryJobConfigDtoServiceImpl implements HistoryTaskConfigurationDtoService {

    @Autowired
    private HistoryTaskConfigDtoRepository historyTaskConfigDtoRepository;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int create(TaskConfigurationDto taskConfigurationDto) throws RuntimeException {
        return historyTaskConfigDtoRepository.insert(taskConfigurationDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int createSelective(TaskConfigurationDto taskConfigurationDto) throws RuntimeException {
        return historyTaskConfigDtoRepository.insertSelective(taskConfigurationDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteByPrimaryKey(Long id) throws RuntimeException {
        return historyTaskConfigDtoRepository.deleteByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    @Override
    public TaskConfigurationDto findByPrimaryKey(Long id) throws RuntimeException {
        TaskConfigurationDto taskConfigurationDto = historyTaskConfigDtoRepository.selectByPrimaryKey(id);
        taskConfigurationDto.setDefaultValues();
        return taskConfigurationDto;
    }

    @Transactional(readOnly = true)
    @Override
    public int selectCount(TaskConfigurationDto taskConfigurationDto) throws RuntimeException {
        return historyTaskConfigDtoRepository.selectCount(taskConfigurationDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByPrimaryKey(TaskConfigurationDto taskConfigurationDto) throws RuntimeException {
        return historyTaskConfigDtoRepository.updateByPrimaryKey(taskConfigurationDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateByPrimaryKeySelective(TaskConfigurationDto taskConfigurationDto) throws RuntimeException {
        return historyTaskConfigDtoRepository.updateByPrimaryKeySelective(taskConfigurationDto);
    }

    @Transactional
    @Override
    public List<TaskConfigurationDto> selectPage(TaskConfigurationDto taskConfigurationDto, Pageable pageable)
            throws RuntimeException {
        List<TaskConfigurationDto> taskConfigurationDtoList = historyTaskConfigDtoRepository.selectPage(taskConfigurationDto, pageable);
        if (taskConfigurationDtoList != null) {
            int i = 1;
            for (TaskConfigurationDto taskConfigurationDto1 : taskConfigurationDtoList) {
                taskConfigurationDto1.setRownum(i++);
                taskConfigurationDto1.setDefaultValues();
            }
        }
        return taskConfigurationDtoList;
    }

}
